const service = require("mysql2");
const moment = require("moment");
const cid = require("@4a/cid");
const connection = service.createConnection({
  // host: "192.168.111.111",
  // user: "root",
  // password: "0125",
  // database: "express",
  host: "localhost",
  user: "root",
  password: "52799454",
  database: "express",
});
//测试
exports.selectAll = (req, res) => {
  connection.query("select * from admin", function (err, results) {
    if (err) {
      console.error("error connecting: " + err.stack);
      return;
    }
    res.send({
      code: "200",
      data: results,
    });
  });
};
//寄件
exports.Sendpiece = (req, res) => {
  console.log(req.body);
  const dt = moment().format("YYYY-MM-DD h:mm:ss");
  let id = cid(10).slice(0, 13);
  let {
    send_name,
    send_phone,
    send_region,
    send_address,
    hav_name,
    hav_phone,
    hav_region,
    hav_address,
    selectval,
    weight,
  } = req.body;
  connection.query(
    `insert into delivery (id , send_name,
    send_tel,
    send_area,
    send_address,
    hav_name,
    hav_tel,
    hav_area,
    hav_address,
    goods_type,
    weight, commit_time,arrival_state) values( '${id}','${send_name}','${send_phone}','${send_region}','${send_address}','${hav_name}','${hav_phone}','${hav_region}','${hav_address}','${selectval}','${weight}','${dt}',0);`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      res.send({
        code: 200,
        data: req.body,
        time: dt,
        id: id,
        message: "订单创建成功",
      });
    }
  );
};
//数据库查询
exports.Waybill = (req, res) => {
  let { cid } = req.query;
  connection.query(
    `SELECT * FROM delivery where id = ${cid}`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      let data = results == 0 ? "无信息" : results[0];
      if (results[0]) {
        let state = ["未发货", "已发货", "已入站", "已签收"];
        results[0].arrival_state = state[results[0].arrival_state];
      }
      res.send({
        code: 200,
        data,
      });
    }
  );
};
//查找省
exports.Networkpointprovince = (req, res) => {
  connection.query(`  SELECT province FROM area`, function (err, results) {
    if (err) {
      console.error("error connecting: " + err.stack);
      return;
    }
    let arr = [];
    //去重
    results.forEach((item) => {
      if (arr.indexOf(item.province) == -1) {
        arr.push(item.province);
      }
    });
    res.send({
      data: 200,
      arr,
    });
  });
};
exports.Networkpointcity = (req, res) => {
  let { province } = req.query;
  connection.query(
    `SELECT city FROM area WHERE province = '${province}' GROUP BY city`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      let arr = [];
      //去重
      results.forEach((item) => {
        if (arr.indexOf(item.city) == -1) {
          arr.push(item.city);
        }
      });
      res.send({
        data: 200,
        arr,
      });
    }
  );
};
//查找区
exports.Networkpointdistrict = (req, res) => {
  let { city } = req.query;
  console.log(res.query);
  connection.query(
    `SELECT district FROM area WHERE city = '${city}' GROUP BY district`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      let arr = [];
      console.log(results);
      //去重
      results.forEach((item) => {
        if (arr.indexOf(item.district) == -1) {
          arr.push(item.district);
        }
      });
      res.send({
        data: 200,
        arr,
      });
    }
  );
};
exports.Networkpointname = (req, res) => {
  let { district } = req.query;
  console.log(res.query);
  connection.query(
    `SELECT address,name,tel FROM area WHERE district='${district}'`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      console.log(results);
      //去重
      res.send({
        data: 200,
        results,
      });
    }
  );
};
//查询运费
exports.Freight = (req, res) => {
  let Price = Number;
  let { ShippingTime, routrname = "", isVip = "" } = req.body;
  let [Origin1, Origin2] =
    req.body.Origin.split("省") || req.body.Origin.split("区");
  let [Destination1, Destination2] =
    req.body.Destination.split("省") || req.body.Origin.split("区");
  console.log(Origin1)
  console.log(Origin2)
  console.log(Destination1)
  console.log(Destination2)
  //判断出发地和目的地同省市区情况
  if (Origin1 === Destination1) {
    //同省
    if (Origin2.split("市")[0] === Destination2.split("市")[0]) {
      //同市
      Price = 8;
    } else {
      //不同市
      Price = 10;
    }
  } else {
    Price = 15;
    //不同省
  }
  //判断物品重量情况
  let weight = parseInt(req.body.Weight);
  switch (true) {
    case weight >= 20:
      Price += 10;
      break;
    case weight >= 10:
      Price += 5;
      break;
    case weight >= 5:
      Price += 2;
      break;
    default:
      break;
  }
  //判断寄件类型
  switch (routrname) {
    case "Valuable":
      Price += 10;
      break;
    case "quick":
      Price += 20;
      break;
    default:
      break;
  }
  //判断vip
  let vipPrice = (data, Price) => {
    if (data && data === "1") {
      return (Price = Price * 0.8);
    }
    return Price;
  };
  //返回
  res.send({
    data: 200,
    res: {
      Price: vipPrice(isVip, Price),
      weight,
      ShippingTime,
    },
  });
};
//登录
exports.login = (req, res) => {
  let { pass, user } = req.body;
  let userinfo = [];
  var message = "";
  connection.query(
    `SELECT * FROM user where user = ${user}`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      userinfo = results;
      if (userinfo.length == 0) {
        message = "用户名不正确";
        res.send({
          code: 200,
          message,
        });
        return;
      } else if (pass !== userinfo[0].pass) {
        message = "密码不正确";
        res.send({
          code: 200,
          message,
        });
        return;
      } else {
        message = "登陆成功";
        res.send({
          code: 200,
          message,
          userinfo: userinfo[0],
        });
        return;
      }
    }
  );
};
//注册
exports.Register = (req, res) => {
  console.log(req.body);
  let { user, pass, username } = req.body;
  connection.query(
    `SELECT * FROM user where user = ${user}`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      console.log(results);
      if (results.length == 0) {
        connection.query(
          `INSERT INTO user(user, pass, is_vip,username) VALUES ('${user}', '${pass}', 0, '${username}')`,
          function (err, results) {
            if (err) {
              console.error("error connecting: " + err.stack);
              return;
            }
          }
        );
        res.send({
          code: 200,
          message: "注册成功",
        });
        return;
      } else {
        res.send({
          code: 200,
          message: "用户名已存在",
        });
        return;
      }
    }
  );
  // connection.query(``)
};
//新闻
exports.News = (req, res) => {
  console.log(req.query);
  let { limt = 2 } = req.query;
  console.log(limt);
  connection.query(
    `SELECT * FROM news LIMIT 0,${limt}`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      res.send({
        code: 200,
        results,
      });
    }
  );
};
//vip
exports.vip = (req, res) => {
  console.log(req.body);
  let { user } = req.query;
  connection.query(
    `UPDATE user SET is_vip = 1 WHERE user = '${user}'`,
    function (err, results) {
      if (err) {
        console.error("error connecting: " + err.stack);
        return;
      }
      res.send({
        code: 200,
        message: "成为会员",
      });
    }
  );
};
